package models;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import play.data.validation.Required;
import play.db.jpa.GenericModel;
import play.db.jpa.Model;

/**
 * This class represents a collaborator mission with its attributes. It is a JPA
 * persistent entity.
 * 
 * @author Elian ORIOU
 * 
 */

@Entity
public class Mission extends GenericModel {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "MISSION_ID")
	private int missionID;

	@Required
	private String title;

	@Required
	@Column(name = "begin_date")
	private Date startDate;

	@Required
	@Column(name = "end_date")
	private Date endDate;

	@Lob
	private String description;

	@OneToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "CLIENT_ID")
	private Client client;

	@ManyToMany
	@JoinTable(name = "MissionSkills", joinColumns = { @JoinColumn(name = "MISSION_ID", referencedColumnName = "MISSION_ID") }, inverseJoinColumns = { @JoinColumn(name = "SKILL_ID", referencedColumnName = "SKILL_ID") })
	private Set<Skill> missionSkills;

	public Mission(String title, Date startDate, Date endDate,
			String description, Client client) {
		super();
		this.missionSkills = new HashSet<Skill>();
		this.title = title;
		this.startDate = startDate;
		this.endDate = endDate;
		this.description = description;
		this.client = client;
	}

	public Mission() {
		super();
	}

	public int getMissionID() {
		return missionID;
	}

	public void setMissionID(int missionID) {
		this.missionID = missionID;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public Set<Skill> getMissionSkills() {
		return missionSkills;
	}

	public void setMissionSkills(Set<Skill> missionSkills) {
		this.missionSkills = missionSkills;
	}

	public void addSkill(Skill skill) {
		skill.save();
		missionSkills.add(skill);
		save();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((client == null) ? 0 : client.hashCode());
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((endDate == null) ? 0 : endDate.hashCode());
		result = prime * result + missionID;
		result = prime * result
				+ ((missionSkills == null) ? 0 : missionSkills.hashCode());
		result = prime * result
				+ ((startDate == null) ? 0 : startDate.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Mission other = (Mission) obj;
		if (client == null) {
			if (other.client != null)
				return false;
		} else if (!client.equals(other.client))
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (endDate == null) {
			if (other.endDate != null)
				return false;
		} else if (!endDate.equals(other.endDate))
			return false;
		if (missionID != other.missionID)
			return false;
		if (missionSkills == null) {
			if (other.missionSkills != null)
				return false;
		} else if (!missionSkills.equals(other.missionSkills))
			return false;
		if (startDate == null) {
			if (other.startDate != null)
				return false;
		} else if (!startDate.equals(other.startDate))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Mission [missionID=" + missionID + ", title=" + title
				+ ", startDate=" + startDate + ", endDate=" + endDate
				+ ", description=" + description + ", client=" + client
				+ ", missionSkills=" + missionSkills + "]";
	}
}
